package com.doxiaopu.students.order.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.order.entity.Order;
import com.doxiaopu.students.order.service.IOrderService;
import com.doxiaopu.students.order.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Api(tags = "订单信息")
@RestController
@CrossOrigin
@RequestMapping("/Order")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
    }

    @ApiOperation(value = "创建订单")
    @PostMapping("/Create")
    @ResponseBody
    public ResponseResult<Long> createOrder(@RequestBody Order order, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        Long data = iOrderService.createOrder(order,id);
        return new ResponseResult<Long>(SUCCESS,data);
    }

    @ApiOperation(value = "查询订单列表")
    @PostMapping("/AllOrderlist")
    @ResponseBody
    public ResponseResult<List<Order>> getOrderList(HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        List<Order> orderlist =  iOrderService.getOrderList(id);
        return new ResponseResult<List<Order>>(SUCCESS,orderlist);
    }

    @ApiOperation(value = "通过订单状态查询订单列表")
    @GetMapping("/AllOrderlistbyType")
    @ResponseBody
    public ResponseResult<List<Order>> getOrderListbytype(Integer types, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        List<Order> orderlist =  iOrderService.getOrderListByType(types,id);
        return new ResponseResult<List<Order>>(SUCCESS,orderlist);
    }

    @ApiOperation(value = "查询管理端订单列表")
    @PostMapping("/AdminOrderlist")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getAdminOrderList(HttpServletRequest request,OrderVO orderVO){
        Integer id =getUidFromRequest(request);
        Map<String, Object> orderlist =  iOrderService.getAdminOrderList(orderVO,id);
        return new ResponseResult<Map<String, Object>>(SUCCESS,orderlist);
    }

    @ApiOperation(value = "通过订单id查询订单列表")
    @PostMapping("/getOrderByOrderId")
    @ResponseBody
    public ResponseResult<Order> getOrderByOrderId(HttpServletRequest request,String orderId){
        Integer id =getUidFromRequest(request);
        System.err.print("orderId:"+orderId+"========");
        Order order =  iOrderService.findOrderById(Long.parseLong(orderId));
        System.err.print("order:"+order+"========");
        return new ResponseResult<Order>(SUCCESS,order);
    }
}
